package com.layui.ad.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.layui.ad.annotation.Log;
import com.layui.ad.entity.SysAdmin;
import com.layui.ad.enums.BusinessType;
import com.layui.ad.query.AdminQuery;
import com.layui.ad.service.SysAdminService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.vo.AdminPwdVo;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Resource
    private SysAdminService sysAdminService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("act", "index");
        request.setAttribute("con", "admin");
        return "admin/index";
    }

    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("admin:list")
    public JsonResult list(AdminQuery adminQuery) {
        return sysAdminService.getList(adminQuery);
    }


    @GetMapping("/edit")
    public String edit(HttpServletRequest request, Integer id) {
        Map<String, Object> info = new HashMap<>();
        if (id != null && id > 0) {
            info = sysAdminService.info(id);
        }
        String roleIds = (String)info.get("roleIds");
        List<Map<String, Object>> role = sysAdminService.getRoleList(roleIds);
        request.setAttribute("info",info);
        request.setAttribute("role",role);
        return "admin/edit";
    }
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("admin:add")
    @Log(title = "管理员",businessType = BusinessType.INSERT)
    public JsonResult add(@RequestBody SysAdmin entiry){
        return sysAdminService.edit(entiry);
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("admin:edit")
    @Log(title = "管理员",businessType = BusinessType.UPDATE)
    public JsonResult update(@RequestBody SysAdmin entiry){
        return sysAdminService.edit(entiry);
    }

    @ResponseBody
    @RequestMapping("/getList")
    public JsonResult getList(HttpServletRequest request, Integer page, Integer limit) {
        String keywords = request.getParameter("keywords");
        IPage<SysAdmin> listData = sysAdminService.getList2(page, limit, keywords);
        List data = listData.getRecords();
        listData.getPages();
        listData.getSize();
        Long total = listData.getTotal();
        return JsonResult.ok(data).set("count", total);
    }

    @ResponseBody
    @PostMapping("/setStatus")
    @Log(title = "管理员",businessType = BusinessType.STATUS)
    public JsonResult setStatus(@RequestBody SysAdmin entiry) {
        return sysAdminService.setStatus(entiry);
    }

    @ResponseBody
    @GetMapping("/batchDelete/{ids}")
    @Log(title = "管理员",businessType = BusinessType.BATCH_DELETE)
    public JsonResult batchDelete(@PathVariable("ids") String ids){
        return sysAdminService.deleteByIds(ids);
    }

    @RequiresPermissions("admin:delete")
    @ResponseBody
    @GetMapping("/delete/{id}")
    @Log(title = "管理员",businessType = BusinessType.DELETE)
    public JsonResult delete(@PathVariable("id") Integer id){
        return sysAdminService.deleteById(id);
    }
}
